package com.example.muchentuner;

import static com.example.muchentuner.ButtonActivity.isAutoModeEnabled;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
import static com.example.muchentuner.ButtonActivity.*;

public class KeyView extends View {

    private Canvas canvas;
    private PitchDifference pitchDifference;
    private float x;
    private float y;
    private float keyboard_w=(float) (110*3.5);
    private float keyboard_h=330f;

    public KeyView(Context context) {
        super(context);
    }

    public KeyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;
        drawKeyBoard();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthSize, heightSize);
        x=widthSize/2;
        y=heightSize/2;
    }

    private void drawKeyBoard(){
        drawRoundRect();

        float wkeyWidth=110f;
        float wkeyHeight=330f;
        float bkeyWidth= (float) (wkeyWidth*0.6);
        float bkeyHeight= (float) (wkeyHeight*0.6);

        boolean lightKey1=false;boolean lightKey2=false;boolean lightKey3=false;boolean lightKey4=false;
        boolean lightKey5=false;boolean lightKey6=false;boolean lightKey7=false;boolean lightKey8=false;
        boolean lightKey9=false;boolean lightKey10=false;boolean lightKey11=false;boolean lightKey12=false;

        if (pitchDifference != null) {
            String pD_name=pitchDifference.closest.getName().getScientific();
            String pD_sign=pitchDifference.closest.getSign();
            Log.v("TAG",pD_name);
            switch (pD_name){
                case "C":
                    if(pD_sign.equals("#")){
                        lightKey2=true;
                    }
                    else lightKey1=true;break;
                case "D":
                    if(pD_sign.equals("#")){
                        lightKey4=true;
                    }
                    else lightKey3=true;break;
                case "E":
                    lightKey5=true;break;
                case "F":
                    if(pD_sign.equals("#")){
                        lightKey7=true;
                    }
                    else lightKey6=true;break;
                case "G":
                    if(pD_sign.equals("#")){
                        lightKey9=true;
                    }
                    else lightKey8=true;break;
                case "A":
                    if(pD_sign.equals("#")){
                        lightKey11=true;
                    }
                    else lightKey10=true;break;
                case "B":
                    lightKey12=true;break;
                default:Log.v("TAG","无");
            }
        }


        Paint paint1=new Paint();
        paint1.setStyle(Paint.Style.FILL);
        paint1.setColor(getResources().getColor(R.color.keyboard));
        //paint1.setStrokeWidth(10f);
        paint1.setAntiAlias(true);
        Paint paint1_stro=new Paint();
        paint1_stro.setStyle(Paint.Style.STROKE);
        paint1_stro.setColor(getResources().getColor(R.color.keyboard_stro));
        paint1_stro.setStrokeWidth(7f);
        paint1_stro.setAntiAlias(true);

        //点亮的白键
        Paint paint_light=new Paint();
        paint_light.setStyle(Paint.Style.FILL);
        paint_light.setColor(getResources().getColor(R.color.lightkey_b));
        paint_light.setAntiAlias(true);
        Paint paint1_light_stro=new Paint();
        paint1_light_stro.setStyle(Paint.Style.STROKE);
        paint1_light_stro.setColor(getResources().getColor(R.color.lightkey_stro));
        paint1_light_stro.setStrokeWidth(5f);
        paint1_light_stro.setAntiAlias(true);
        //点亮的白键

        Path path1 = new Path();
        path1.moveTo((float) (x-3.5*wkeyWidth),(float) (y-0.5*wkeyHeight));//1
        path1.lineTo((float) (x-3.5*wkeyWidth+(wkeyWidth-0.65*bkeyWidth)),(float) (y-0.5*wkeyHeight));
        path1.lineTo((float) (x-3.5*wkeyWidth+(wkeyWidth-0.65*bkeyWidth)),(float) ((y-0.5*wkeyHeight)+bkeyHeight));
        path1.lineTo((float) (x-3.5*wkeyWidth+wkeyWidth),(float) ((y-0.5*wkeyHeight)+bkeyHeight));//4
        path1.lineTo((float) (x-3.5*wkeyWidth+wkeyWidth),(float) (y+0.5*wkeyHeight));//5
        path1.lineTo((float) (x-3.5*wkeyWidth),(float) (y+0.5*wkeyHeight));//6
        path1.close();
        if(lightKey1){
            canvas.drawPath(path1,paint_light);
            canvas.drawPath(path1,paint1_light_stro);
        }
        else{
            canvas.drawPath(path1,paint1);
            canvas.drawPath(path1,paint1_stro);
        }

        Path path2 = new Path();
        path2.moveTo((float) (x-2.5*wkeyWidth+0.35*bkeyWidth),(float) (y-0.5*wkeyHeight));//1
        path2.lineTo((float) (x-2.5*wkeyWidth+(wkeyWidth-0.35*bkeyWidth)),(float) (y-0.5*wkeyHeight));
        path2.lineTo((float) (x-2.5*wkeyWidth+(wkeyWidth-0.35*bkeyWidth)),(float) ((y-0.5*wkeyHeight)+bkeyHeight));
        path2.lineTo((float) (x-2.5*wkeyWidth+wkeyWidth),(float) ((y-0.5*wkeyHeight)+bkeyHeight));//4
        path2.lineTo((float) (x-2.5*wkeyWidth+wkeyWidth),(float) (y+0.5*wkeyHeight));//5
        path2.lineTo((float) (x-2.5*wkeyWidth),(float) (y+0.5*wkeyHeight));//6
        path2.lineTo((float) (x-2.5*wkeyWidth),(float) ((y-0.5*wkeyHeight)+bkeyHeight));//7
        path2.lineTo((float) (x-2.5*wkeyWidth+0.35*bkeyWidth),(float) ((y-0.5*wkeyHeight)+bkeyHeight));//8
        path2.close();
        if(lightKey3){
            canvas.drawPath(path2,paint_light);
            canvas.drawPath(path2,paint1_light_stro);
        }
        else{
            canvas.drawPath(path2,paint1);
            canvas.drawPath(path2,paint1_stro);
        }

        Path path3 = new Path();
        path3.moveTo((float) (x-1.5*wkeyWidth+0.65*bkeyWidth),(float) (y-0.5*wkeyHeight));//1
        path3.lineTo((float) (x-1.5*wkeyWidth+wkeyWidth),(float) (y-0.5*wkeyHeight));
        path3.lineTo((float) (x-1.5*wkeyWidth+wkeyWidth),(float) (y+0.5*wkeyHeight));
        path3.lineTo((float) (x-1.5*wkeyWidth),(float) (y+0.5*wkeyHeight));//4
        path3.lineTo((float) (x-1.5*wkeyWidth),(float) ((y-0.5*wkeyHeight)+bkeyHeight));//5
        path3.lineTo((float) (x-1.5*wkeyWidth+0.65*bkeyWidth),(float) ((y-0.5*wkeyHeight)+bkeyHeight));//6
        path3.close();
        if(lightKey5){
            canvas.drawPath(path3,paint_light);
            canvas.drawPath(path3,paint1_light_stro);
        }
        else{
            canvas.drawPath(path3,paint1);
            canvas.drawPath(path3,paint1_stro);
        }

        //白键4~7
        Matrix m=new Matrix();
        m.postTranslate(3*wkeyWidth,0);
        path1.transform(m);
        if(lightKey6){
            canvas.drawPath(path1,paint_light);
            canvas.drawPath(path1,paint1_light_stro);
        }
        else{
            canvas.drawPath(path1,paint1);
            canvas.drawPath(path1,paint1_stro);
        }

        m.reset();
        m.postTranslate(3*wkeyWidth,0);
        path2.transform(m);
        if(lightKey8){
            canvas.drawPath(path2,paint_light);
            canvas.drawPath(path2,paint1_light_stro);
        }
        else{
            canvas.drawPath(path2,paint1);
            canvas.drawPath(path2,paint1_stro);
        }

        m.reset();
        m.postTranslate(wkeyWidth,0);
        path2.transform(m);
        if(lightKey10){
            canvas.drawPath(path2,paint_light);
            canvas.drawPath(path2,paint1_light_stro);
        }
        else{
            canvas.drawPath(path2,paint1);
            canvas.drawPath(path2,paint1_stro);
        }

        m.reset();
        m.postTranslate(4*wkeyWidth,0);
        path3.transform(m);
        if(lightKey12){
            canvas.drawPath(path3,paint_light);
            canvas.drawPath(path3,paint1_light_stro);
        }
        else{
            canvas.drawPath(path3,paint1);
            canvas.drawPath(path3,paint1_stro);
        }

        Paint paint_b=new Paint();
        paint_b.setStyle(Paint.Style.FILL);
        paint_b.setColor(getResources().getColor(R.color.keyboard_black));
        //paint_b.setStrokeWidth(10f);
        paint_b.setAntiAlias(true);
        Paint paint_b_stro=new Paint();
        paint_b_stro.setStyle(Paint.Style.STROKE);
        paint_b_stro.setColor(getResources().getColor(R.color.keyboard_stro));
        paint_b_stro.setStrokeWidth(5f);
        paint_b_stro.setAntiAlias(true);

        //点亮的黑键
        Paint paint_light_b=new Paint();
        paint_light_b.setStyle(Paint.Style.FILL);
        paint_light_b.setColor(getResources().getColor(R.color.lightkey_b));
        paint_light_b.setAntiAlias(true);
        Paint paint_light_b_stro=new Paint();
        paint_light_b_stro.setStyle(Paint.Style.STROKE);
        paint_light_b_stro.setColor(getResources().getColor(R.color.lightkey_stro));
        paint_light_b_stro.setStrokeWidth(7f);
        paint_light_b_stro.setAntiAlias(true);
        //点亮的黑键

        Path b_path=new Path();
        b_path.moveTo((float) (x-2.5*wkeyWidth-0.65*bkeyWidth),(float) (y-wkeyHeight*0.5));
        b_path.lineTo((float) (x-2.5*wkeyWidth-0.65*bkeyWidth+bkeyWidth),(float) (y-wkeyHeight*0.5));
        b_path.lineTo((float) (x-2.5*wkeyWidth-0.65*bkeyWidth+bkeyWidth),(float) (y-wkeyHeight*0.5+bkeyHeight));
        b_path.lineTo((float) (x-2.5*wkeyWidth-0.65*bkeyWidth),(float) (y-wkeyHeight*0.5+bkeyHeight));
        b_path.close();
        if(lightKey2){
            canvas.drawPath(b_path,paint_light_b);
            canvas.drawPath(b_path,paint_light_b_stro);
        }
        else{
            canvas.drawPath(b_path,paint_b);
            canvas.drawPath(b_path,paint_b_stro);
        }

        Matrix m_b=new Matrix();
        m_b.postTranslate((float) (wkeyWidth+0.35*bkeyWidth),0);
        b_path.transform(m_b);
        if(lightKey4){
            canvas.drawPath(b_path,paint_light_b);
            canvas.drawPath(b_path,paint_light_b_stro);
        }
        else{
            canvas.drawPath(b_path,paint_b);
            canvas.drawPath(b_path,paint_b_stro);
        }

        m_b.reset();
        m_b.postTranslate((float) (2*wkeyWidth-0.35*bkeyWidth),0);
        b_path.transform(m_b);
        if(lightKey7){
            canvas.drawPath(b_path,paint_light_b);
            canvas.drawPath(b_path,paint_light_b_stro);
        }
        else{
            canvas.drawPath(b_path,paint_b);
            canvas.drawPath(b_path,paint_b_stro);
        }

        m_b.reset();
        m_b.postTranslate((float) (wkeyWidth+0.15*bkeyWidth),0);
        b_path.transform(m_b);
        if(lightKey9){
            canvas.drawPath(b_path,paint_light_b);
            canvas.drawPath(b_path,paint_light_b_stro);
        }
        else{
            canvas.drawPath(b_path,paint_b);
            canvas.drawPath(b_path,paint_b_stro);
        }
        m_b.reset();
        m_b.postTranslate((float) (wkeyWidth+0.15*bkeyWidth),0);
        b_path.transform(m_b);
        if(lightKey11){
            canvas.drawPath(b_path,paint_light_b);
            canvas.drawPath(b_path,paint_light_b_stro);
        }
        else{
            canvas.drawPath(b_path,paint_b);
            canvas.drawPath(b_path,paint_b_stro);
        }

    }

    private void drawRoundRect(){
        Paint p=new Paint();
        p.setStyle(Paint.Style.FILL);//充满
        p.setColor(getResources().getColor(R.color.keyview_backRect));
        p.setAntiAlias(true);// 设置画笔的锯齿效果
        RectF RRectf = new RectF(x-keyboard_w-70, y-keyboard_h/2-40, x+keyboard_w+70, y+keyboard_h/2+40);// 设置个新的长方形
        canvas.drawRoundRect(RRectf, 45, 45, p);//第二个参数是x半径，第三个参数是y半径
    }

    public void setPitchDifference(PitchDifference pitchDifference) {
        this.pitchDifference = pitchDifference;
    }

    private int getNearestDeviation() {
        float deviation = (float) pitchDifference.deviation;
        int rounded = Math.round(deviation);
        //return Math.round(rounded / 10f) * 10;
        return rounded;
    }
}
